package com.cat.dynamicPlanning;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/palindrome-partitioning-ii/
 * @create 2025/9/4 22:10
 * @since JDK17
 */

public class Solution73 {
    public int minCut(String s) {
        int n = s.length();
        boolean[][] dp = new boolean[n + 1][n + 1];
        for (int i = 0; i < n; i++) {
            Arrays.fill(dp[i], true);
        }
        for (int i = n - 2; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                dp[i][j] = s.charAt(i) == s.charAt(j) && dp[i + 1][j - 1];
            }
        }
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            ans[i] = i;
        }

        for (int i = 1; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                if (dp[j][i]) {
                    ans[i] = Math.min(ans[i], j > 0 ? 1 + ans[j - 1] : 0);
                }
            }
        }

        return ans[n - 1];
    }
}
